<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      left-arrow
      @click-left="$router.go(-1)"
      title="文章详情"
      fixed
    ></van-nav-bar>
    <!-- 文章详情 -->
    <div class="detail" v-if="finished">
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | relative}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="toggleFollow"
        >{{article.is_followed ? '取关' : '+ 关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content">正文</div>
      </div>
      <van-divider>分割线</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job': 'good-job-o'"
          @click="toggleGood"
        >{{article.attitude === 1 ? '取消点赞' : '点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
      </div>
    </div>
    <!-- /文章详情 -->
    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-else/>
    <!-- /加载中 loading -->

    <!-- 评论组件 -->
    <comment></comment>
  </div>
</template>

<script>
import Comment from './comment'
import { getArticleDetail, addFollow, delFollow, addGood, delGood } from '@/api/article'
export default {
  name: 'Article',
  components: {
    Comment
  },
  computed: {
    token () {
      return this.$store.state.token.token
    }
  },
  data () {
    return {
      article: {},
      finished: false
    }
  },
  /*
    1. 获取地址栏的id
    2. 封装接口，获取文章详情
    3. 调用接口，获取到文章详情数据
    4. 渲染文章详情数据即可
  */
  destroyed () {
    console.log('详情组件销毁')
  },
  async created () {
    console.log('详情组件创建')
    console.log(this.$route, 'article')
    const id = this.$route.query.id
    const res = await getArticleDetail(id)
    this.article = res.data
    console.log(res.data)
    this.finished = true
  },
  methods: {
    async toggleFollow () {
      /*
        1. 判断用户是否登录
        2. 如果用户没有登录，跳转到登录页
        3. 如果用户登录了，判断是否关注
        4. 如果关注了，发送请求取消关注
        5. 如果没有关注，发送请求关注
      */
      if (!this.token) {
        // 跳转到登录页
        this.$router.push({
          path: '/login',
          query: {
            back: this.$route.fullPath
          }
        })
        return this.$toast.fail('请先登录')
      }
      if (this.article.is_followed) {
        // 关注了
        // console.log('取关')
        await delFollow(this.article.aut_id)
        this.$toast.success('取关成功')
        this.article.is_followed = false
      } else {
        await addFollow(this.article.aut_id)
        this.$toast.success('关注成功')
        this.article.is_followed = true
      }
    },
    async toggleGood () {
      // 判断是否登录
      if (!this.token) {
        // 跳转到登录页
        this.$router.push({
          path: '/login',
          query: {
            back: this.$route.fullPath
          }
        })
        return this.$toast.fail('请先登录')
      }
      // 判断是否点赞
      if (this.article.attitude === 1) {
        console.log('取消点赞')
        await delGood(this.article.art_id.toString())
        this.article.attitude = 0
      } else {
        await addGood(this.article.art_id.toString())
        this.article.attitude = 1
      }
    }
  }
}
</script>

<style lang='less' scoped>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
